/*
 * @Author: 肖思汗 
 * @Date: 2025-07-16 15:03:47 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-08-23 22:04:08
 */

import utils from "@/utils/utils";
import axios, { AxiosResponse } from "axios";

// 请求的基类
export default class Api {

    constructor() {
    }

    //oss地域
    static oss_host = process.env.oss_host;

    //开发模式访问的域名
    baseURL = (() => {
        if (utils.isDev) {
            // 开发模式的接口地址
            return "http://localhost:8081/";
        } else if (utils.isTest) {
            // 测试环境的接口地址
            return "https://xsh-demo.3dkeshihua.com/";
        } else if (utils.isProd) {
            // 正式环境的接口地址
            return "https://xsh-demo.3dkeshihua.com/";
        }
    })();

    //超时时间
    timeout = 10 * 60 * 1000;

    // 创建 请求实例
    _createInstance(headers = {}) {
        const axioxInstance = axios.create({
            baseURL: this.baseURL,
            timeout: this.timeout,
            headers: {
                ...headers
            },
        });

        // 请求拦截器
        axioxInstance.interceptors.request.use(
            (config) => {
                // 部分接口需要拿到Token
                let token = utils.getStorageSync('token');
                if (token) {
                    config.headers!.authorization = `Bearer ${token}`;
                }
                config.data = config.data;
                return config;
            },
            (err: any) => {
                return Promise.reject(err)
            });

        // 响应拦截器
        axioxInstance.interceptors.response.use(
            (res: AxiosResponse): AxiosResponse => {
                // let code = get(res, ['data', 'code']);
                // if ([401, 402, 403].includes(code)) {
                // adminUserStore.setIsLogin(false);
                // if (utils.isDev) {
                //     adminUserStore.login("admin", "aaadmin123!");
                // } else {
                utils.removeStorageSync('token');
                // }
                // } else if ([500].includes(code)) {
                // message.error(res.data.msg);
                // }
                return res.data;
            },
            (err: any) => {
                // let code = get(err, ['response', 'data', 'code']);
                // if ([401, 402, 403].includes(code)) {
                // adminUserStore.setIsLogin(false);
                // if (utils.isDev) {
                //     adminUserStore.login("admin", "aaadmin123!");
                // } else {
                utils.removeStorageSync('token');
                // }
                // } else if ([200].includes(code)) {
                // } else {
                // return Promise.reject(new Error(get(err, ['response', 'data', 'message'])));
                // }
            }
        );
        return axioxInstance;
    }

    // get请求
    _get(url: string, params?: {}): Promise<any> {
        const axioxInstance = this._createInstance();
        return axioxInstance.get(url, { params });
    }

    // post 请求
    _post(url: string, data = {}): Promise<any> {
        const axioxInstance = this._createInstance();
        return axioxInstance.post(url, data);
    }

    // 上传
    _upload(url: string, file: any): Promise<any> {
        const axioxInstance = this._createInstance();
        return axioxInstance.post(url, file);
    }

    //表单提交
    _form(url: string, data: any = {}): Promise<any> {
        const formData = new FormData();
        for (let key in data) {
            formData.append(key, data[key]);
        }
        const axioxInstance = this._createInstance();
        return axioxInstance.post(url, formData);
    }

}
